// subpack/pages/detailPublish/detailPublish.js
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        lost: null,
        whereFrom: "",
        totleComment: 0,
        commentLists: null,
        isLogin: 0,

        cid:0,
        replyName:null,
        pid:0,
        index:0,
        placeholder: '说点什么...',
        comment_text: null,
    },
    
    commentChange(e){
      console.log(e);
      var value = e.detail.value;
      // console.log(value);
      this.setData({comment_text: value})
    },
    confirmComment(){
      // console.log(this.data.comment_text);
      var _this = this;
      var comment = {
        lostId: this.data.lost.lostId,
        commentBody: this.data.comment_text,
        replyId: 0,
        parentId: 0
      }
      console.log(this.data.cid);
      if(this.data.cid != 0){
        comment.replyId = this.data.cid;
        if(this.data.pid != 0) comment.parentId = this.data.pid;
        else comment.parentId = this.data.cid;
      }
      wx.request({
        url: app.globalData.URL_PRE + "/comment/comments",
        method: "POST",
        data: JSON.stringify(comment),
        header: {
          "cookie": wx.getStorageSync('cookie')
        },
        success: (res)=>{
            console.log(res.data.data);
            if(res.data.code === 200){
               var newcomment = res.data.data;
               if(newcomment.parentId != 0) newcomment.replyUsername = _this.data.replyName;
               newcomment.userName = app.globalData.userInfo.userName;
               newcomment.userAvatar = app.globalData.userInfo.userAvatar;
               newcomment.index = _this.data.index;
               console.log(newcomment);
               var list = _this.data.commentLists;
               if(newcomment.parentId != 0){
                 list[_this.data.index].push(newcomment);
               }
               else{
                 var newList = new Array();
                 newList.push(newcomment);
                 list.push(newList);
               }
               
               var tot = _this.data.totleComment +1;
               console.log(list);
               _this.setData({
                 commentLists: list,
                 index: list.length,
                 cid:0,
                 replyName:null,
                 pid:0,
                 placeholder: '说点什么...',
                 comment_text: null,
                 totleComment: tot
               })
               
            }
            else{
                wx.showToast({
                  title: "请求失败，请重试",
                  icon: "error",
                  duration: 1500
                })
            }
        }
      })
    },

    replyComment(event){
      var cid = event.currentTarget.dataset.cid; //当前点击的评论id
      var name = event.currentTarget.dataset.name; //当前点击的评论昵称
      var pid = event.currentTarget.dataset.pid; //当前点击的评论所属评论id
      var index = event.currentTarget.dataset.index; // 在list 的 pos
      this.setData({
        cid: cid,
        replyName: name,
        pid: pid,
        index: index,
        placeholder: '回复'+name
      })
      console.log(this.data);
    },
  
    getCommentList(){
      var _this = this;
        wx.request({
          url: app.globalData.URL_PRE + "/comment/comments?lostId=" + _this.data.lost.lostId,
          method: "GET",
          header: {
            "cookie": wx.getStorageSync('cookie')
          },
          success: (res)=>{
              console.log(res.data.data);
              if(res.data.code === 200){
                  _this.setData({commentLists: res.data.data});
                  var tot = 0;
                  // 总评论
                  for(let i = 0; i < _this.data.commentLists.length; i++) tot += _this.data.commentLists[i].length;
                  this.setData({
                    totleComment: tot,
                    index: _this.data.commentLists.length
                  });
              }
              else{
                  wx.showToast({
                    title: "请求失败，请重试",
                    icon: "error",
                    duration: 1500
                  })
              }
          }
        })
    },

    confirmBut(){
      console.log(this.data.whereFrom);
        if(this.data.whereFrom === "publish"){
          wx.navigateBack({
            delta: 2,
          })
        }else if(this.data.whereFrom === "myPublish"){
          wx.navigateBack({
            delta: 1,
          })
        }
      
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // console.log(options);

        this.setData({
          whereFrom: options.whereFrom
        })
        wx.setNavigationBarTitle({
          title: '详细信息',
        })
        // 根据lostId查找
        var _this = this;
        wx.request({
          url: app.globalData.URL_PRE + "/getPublishByLostId?lostId=" + options.lostId,
          method: "GET",
          header: {
            "cookie": wx.getStorageSync('cookie')
          },
          success: (res)=>{
            //   console.log(res.data.data);
              if(res.data.code === 200){
                  _this.setData({ lost : res.data.data})
                  _this.setData({
                      ['lost.birthday']: res.data.data.birthday.slice(0, 10),
                      ['lost.lostDate']: res.data.data.lostDate.slice(0, 10),
                  })
                  _this.getCommentList();
              }
              else{
                  wx.showToast({
                    title: "请求失败，请重试",
                    icon: "error",
                    duration: 1500
                  })
              }
          }
        })
        if(app.globalData.userInfo != null) this.setData({isLogin: 1})
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})